package ajax17;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import com.mysql.jdbc.Driver;

public class member {
	String userid;
	String nickname;
	String pw1;
	String pw2;
	String email;
	String useridmsg;
	String nicknamemsg;
	String pw1msg;
	String pw2msg;
	String emailmsg;
	
	public boolean validate(){
		boolean valid = true;
		
		if (userid.length() < 4 || userid.length() > 20) {
			valid = false;
			useridmsg = "帳號長度不符合!!";
		}
		
		if (nickname.length() < 4 || nickname.length() > 20) {
			valid = false;
			nicknamemsg = "暱稱長度不符合!!";
		}
		
		if (!pw1.equals(pw2)) {
			valid = false;
			pw1msg = "密碼必要相同!!";
			pw2msg = "密碼必要相同!!";
		}
	
		if (email.indexOf("@") == -1){
			valid = false;
			emailmsg = "電子郵件帳號長度不符合!!";
		}
		
		if (valid) {
//			saveOld();
						
			int rowcount=0;
			try {
				DriverManager.registerDriver(new Driver());
				Connection conn = DriverManager.getConnection(
						"jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
				String sql = "insert into users(userid, nickname, password, email) values (?, ?, ?, ?)";
				PreparedStatement pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, userid);
				pstmt.setString(2, nickname);
				pstmt.setString(3, pw1);
				pstmt.setString(4, email);
				rowcount = pstmt.executeUpdate();
				System.out.println(rowcount);
				
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				if (rowcount != 1){
					System.out.println("ERROR !!");
				}
			}
			
		}
	return valid;
	}

	private void saveOld() {
		try {
			// 1
			DriverManager.registerDriver(new Driver());
			// 2
			Connection conn = DriverManager.getConnection(
				"jdbc:mysql://j.snpy.org/j102", "jstu", "abc123");
			Statement stmt = conn.createStatement();
			String sql = 
					"INSERT INTO users(id, nickname, password, email) values "
							+ "('" + userid + "', '" + nickname + "', '" + pw1 + "', '" + email + "' )";
			System.out.println(sql);
			int rowcount = stmt.executeUpdate(sql);
			System.out.println(rowcount);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
			
	public member() {
		
	}

	public member(String userid, String nickname, String pw1, String pw2,
			String email) {
		super();
		this.userid = userid;
		this.nickname = nickname;
		this.pw1 = pw1;
		this.pw2 = pw2;
		this.email = email;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	public String getNickname() {
		return nickname;
	}

	public void setNickname(String nickname) {
		this.nickname = nickname;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getPw2() {
		return pw2;
	}

	public void setPw2(String pw2) {
		this.pw2 = pw2;
	}

	public String getUseridmsg() {
		return useridmsg;
	}

	public void setUseridmsg(String useridmsg) {
		this.useridmsg = useridmsg;
	}

	public String getNicknamemsg() {
		return nicknamemsg;
	}

	public void setNicknamemsg(String nicknamemsg) {
		this.nicknamemsg = nicknamemsg;
	}

	public String getPw1msg() {
		return pw1msg;
	}

	public void setPw1msg(String pw1msg) {
		this.pw1msg = pw1msg;
	}

	public String getPw2msg() {
		return pw2msg;
	}

	public void setPw2msg(String pw2msg) {
		this.pw2msg = pw2msg;
	}

	public String getEmailmsg() {
		return emailmsg;
	}

	public void setEmailmsg(String emailmsg) {
		this.emailmsg = emailmsg;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
}
